﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.Api;
using Portal.FileSystemData;

namespace Portal.Modules.FileBrowser
{
  [AjaxModule()]
  [EditModule()]
  public partial class ModuleEditFileBrowser : Portal.Api.Module
  {
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          ModuleConfig cfg = (ModuleConfig)Document.ReadConfig(typeof(ModuleConfig));
          if (cfg == null)
            cfg = new ModuleConfig();
          _virtualRootEdit.Text = cfg.VirtualRoot;

          _pageSizeEdit.Text = cfg.FilesPerPage.ToString();

          _publishAsRssCheck.Checked = cfg.EnableRssFeed;
          _rssTitleEdit.Text = cfg.RssFeedTitle;
          _rssDescriptionEdit.Text = cfg.RssFeedDescription;
          _rssTimeToLive.Text = cfg.RssFeedTimeToLive.ToString();

          _linkPosCombo.Items.Add(new ListItem(Portal.Api.Language.GetText(Portal.Api.Module.GetModuleControl(this),
                                                              "NoLink"), "00"));
          _linkPosCombo.Items.Add(new ListItem(Portal.Api.Language.GetText(Portal.Api.Module.GetModuleControl(this),
                                                    "LinkOnTop"), "01"));
          _linkPosCombo.Items.Add(new ListItem(Portal.Api.Language.GetText(Portal.Api.Module.GetModuleControl(this),
                                           "LinkOnBottom"), "10"));
          _linkPosCombo.Items.Add(new ListItem(Portal.Api.Language.GetText(Portal.Api.Module.GetModuleControl(this),
                                 "LinkOnTopAndBottom"), "11"));
          _linkPosCombo.SelectedValue = string.Format("{0}{1}", cfg.RssLinkOnBottom ? 1 : 0, cfg.RssLinkOnTop ? 1 : 0);

          EnableRss(_publishAsRssCheck.Checked);
        }
      }

      protected void SaveLB_Click(object sender, System.EventArgs e)
      {
        FileBrowser.ModuleConfig cfg = (FileBrowser.ModuleConfig)Document.ReadConfig(typeof(FileBrowser.ModuleConfig));
        if (cfg == null)
        {
          cfg = new ModuleConfig();
        }

        cfg.VirtualRoot = _virtualRootEdit.Text;
        if(string.IsNullOrEmpty(_pageSizeEdit.Text))
          cfg.FilesPerPage = 0;
        else
          cfg.FilesPerPage = int.Parse(_pageSizeEdit.Text);

        cfg.EnableRssFeed = _publishAsRssCheck.Checked;
        cfg.RssFeedTitle = _rssTitleEdit.Text;
        cfg.RssFeedDescription = _rssDescriptionEdit.Text;
        if (string.IsNullOrEmpty(_rssTimeToLive.Text))
          cfg.RssFeedTimeToLive = 60;
        else
          cfg.RssFeedTimeToLive = int.Parse(_rssTimeToLive.Text);

        cfg.RssLinkOnTop = _linkPosCombo.SelectedValue[1] == '1';
        cfg.RssLinkOnBottom = _linkPosCombo.SelectedValue[0] == '1';

        Document.WriteConfig(cfg);
        RedirectBack();
      }

      protected void CancelLB_Click(object sender, System.EventArgs e)
      {
        RedirectBack();
      }

      protected void PublishAsRssCheck(object sender, EventArgs e)
      {
        EnableRss( _publishAsRssCheck.Checked);
        if (_publishAsRssCheck.Checked && _linkPosCombo.SelectedValue == "00")
          _linkPosCombo.SelectedValue = "01"; // Default value.
      }

      private void EnableRss(bool enableRss)
      {
        _linkPosCombo.Enabled = enableRss;
        _rssTitleLbl.Enabled = enableRss;
        _rssTitleEdit.Enabled = enableRss;
        _rssDescriptionLbl.Enabled = enableRss;
        _rssDescriptionEdit.Enabled = enableRss;
        _rssTimeToLiveLbl.Enabled = enableRss;
        _rssTimeToLive.Enabled = enableRss;
        _rssTimeToLive_NumericUpDownExtender.Enabled = enableRss;
        _rssTimeToLiveMinLbl.Enabled = enableRss;
      }
  }
}
